﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FubiManger.Controls;
using Model;
using DAL;

namespace FubiManger.UI
{
    partial class AddpacketForm : Smobiler.Core.Controls.MobileForm
    {
        public AddpacketForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 规则ID
        /// </summary>
        private string ruleid { get; set; }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 时间单位
        /// </summary>
        private string unit { get; set; }
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 选择日期
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panday_Press(object sender, EventArgs e)
        {
            calendcontrol con = new calendcontrol();
            this.Form.ShowDialog(con, (obj, args) =>
            {
                lblday.Text = con.days;
            });
        }
        /// <summary>
        /// 小时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panhour_Press(object sender, EventArgs e)
        {
            Edittimecontrol con = new Edittimecontrol();
            this.Form.ShowDialog(con, (obj, args) =>
            {
                if (Convert.ToInt32(con.hour) > 0)
                    lblhour.Text = con.hour + ":" + con.miniute;
            });
        }
        /// <summary>
        /// 选择时长单位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panchoosehour_Press(object sender, EventArgs e)
        {
            choosehourcontrol con = new choosehourcontrol();
            this.Form.ShowDialog(con, (obj, args) =>
            {
                lbltimer.Text = con.unit;//时长单位
            });
        }
        /// <summary>
        /// 添加红包
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pansave_Press(object sender, EventArgs e)
        {
            ms_packet packet = new ms_packet();
            DateTime starttime = Convert.ToDateTime(lblday.Text + " " + lblhour.Text);//起始时间
            packet.p_starttime = lblday.Text + " " + lblhour.Text;//开始时间
            packet.p_merchantid = merchantid;//商户ID
            packet.p_money = txtmoney.Text;//红包金额
            packet.p_often = txtoften.Text;//活动时长
            packet.p_rank = txtrank.Text;//范围距离
            packet.p_stype = "1";//红包类型
            
            packet.p_level = level;//等级
            packet.p_ruleid = ruleid;//规则ID
            if (lbltype.Text == "随机红包")
                packet.p_kettype = "1";
            else
                packet.p_kettype = "2";
            if (lblrank.Text == "本地")
                packet.p_way = "2";
            else
                packet.p_way = "1";
            if (lbltimer.Text == "小时")
            {
                packet.p_unit = "1";//时间单位
                packet.p_endtime = starttime.AddHours(Convert.ToInt32(txtoften.Text)).ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                packet.p_unit = "2";//时间单位
                packet.p_endtime = starttime.AddMinutes(Convert.ToInt32(txtoften.Text)).ToString("yyyy-MM-dd HH:mm:ss");
            }
            packet.p_ketcount = txtcount.Text;//红包个数
            try
            {
                if (packetDal.Addpacket(packet) > 0)
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
               // Toast("参数异常");
            }

        }

        private void AddpacketForm_Load(object sender, EventArgs e)
        {
            //merchantid = "2";
            lblday.Text = DateTime.Now.ToString("yyyy/MM/dd");
        }
        /// <summary>
        /// 设置红包类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panstype_Press(object sender, EventArgs e)
        {
            packettypecontrol con = new packettypecontrol();
            this.Form.ShowDialog(con, (obj, args) =>
            {
                if (!string.IsNullOrEmpty(con.p_kettype))
                {
                    lbltype.Text = con.p_kettype;
                }
            });
        }
        /// <summary>
        /// 选择范围
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panrank_Press(object sender, EventArgs e)
        {
            packetrankcontrol con = new packetrankcontrol();
            this.Form.ShowDialog(con, (obj, args) =>
            {
                lblrank.Text = con.p_rank;//范围
                if (lblrank.Text == "本地")
                {
                    txtrank.Visible = true;
                    lbl_rank.Visible = true;
                }
                else
                {
                    txtrank.Visible = false;
                    lbl_rank.Visible = false;
                }
            });
        }
        string level = "1";
        /// <summary>
        /// 选择等级
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panlevel_Press(object sender, EventArgs e)
        {
            PacketlevelForm frm = new PacketlevelForm();
            this.Form.Show(frm, true, (obj, args) =>
            {
                if (!string.IsNullOrEmpty(frm.level))
                {
                    lbllevel.Text = frm.level+"级";
                    level = frm.level;
                }
            });
        }

        private void panrule_Press(object sender, EventArgs e)
        {
            AddruleForm frm = new AddruleForm();
            this.Form.Show(frm, (obj, args) =>
            {
                if (!string.IsNullOrEmpty(frm.ruleid))
                {
                    lblruletitle.Text = frm.ruletitle;
                    ruleid = frm.ruleid;//规则ID
                }
            });
        }
        /// <summary>
        /// 红包数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtcount_TouchLeave(object sender, EventArgs e)
        {
            try
            {
                int count = Convert.ToInt32(txtcount.Text);
                txtcount.Text = count.ToString();
            }
            catch (Exception)
            {

                txtcount.Text = "0";
            }

        }
        /// <summary>
        /// 设置金额
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtmoney_TouchLeave(object sender, EventArgs e)
        {
            try
            {
                int count = Convert.ToInt32(txtmoney.Text);
                txtmoney.Text = count.ToString();
                lblmoney.Text = count.ToString();
            }
            catch (Exception)
            {

                txtmoney.Text = "0";
            }
        }
    }
}